const os = require('os');

// 获取本地 ip
function getIp () {
  var interfaces = os.networkInterfaces()
  var addresses = []
  for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
      var address = interfaces[k][k2]
      if (address.family === 'IPv4' && !address.internal) {
        addresses.push(address.address)
      }
    }
  }
  return addresses[0]
}

// 对消息鉴权 or 过滤
function auth (msg) {
  const ip = getIp();

  // 参数类型判断
  if(Object.prototype.toString.call(msg) !== '[object Object]') {
    return false;
  }

  // 目标 ip 判断
  if(!msg.ip || msg.ip !== ip) {
    return false;
  }
  return true
}

module.exports = {
	getIp,
  auth
}